package com.mf.all.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mf.all.emuns.EnumBaseParams;
import com.mf.all.entity.dto.DtoRolePermission;
import com.mf.all.entity.req.user.ReqUserByRole;
import com.mf.all.service.ServiceUser;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import com.mf.all.entity.dto.DtoUserRole;
import com.mf.all.mapper.MapperUserRole;
import com.mf.all.service.ServiceUserRole;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class ImplServiceUserRole extends ServiceImpl<MapperUserRole, DtoUserRole> implements ServiceUserRole {
    @Resource
    private MapperUserRole mapperUserRole;


    /**
     * 角色关联用户
     */
    @Override
    @Transactional
    public void userRoleRelevance(ReqUserByRole reqUserByRole) {
        // 删除角色角色下关联用户
        deleteUserRole(reqUserByRole.getRoleId());
        final String name = (String) StpUtil.getSession().get(EnumBaseParams.douser.name());
        reqUserByRole.setDouser(name);
        mapperUserRole.userRoleRelevance(reqUserByRole);
    }

    /**
     * 删除角色关联用户
     */
    @Transactional
    public void deleteUserRole(Integer roleId) {
        final LambdaQueryWrapper<DtoUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtoUserRole::getRoleId, roleId);
        this.remove(wrapper);
    }
}